﻿using Microsoft.Practices.Unity;

namespace CommonPatterns.MVVM
{
    /// <summary>
    /// Extension methods for the Ioc Container, according to the MVVM patern
    /// </summary>
    public static class MvvmExtension
    {
        /// <summary>
        /// This methods controls the registration of the View, ViewModel elements
        /// </summary>
        /// <typeparam name="View"></typeparam>
        /// <typeparam name="ViewModel"></typeparam>
        /// <typeparam name="IViewModel">interface of the ViewModel</typeparam>
        /// <param name="container"></param>
        /// <param name="isUnique">denotes if the instance being registered is Unique or not </param>
        public static void RegisterMvvmUnit<View, ViewModel, IViewModel>(this IUnityContainer container, bool isUnique)
            where ViewModel : IViewModel
        {
            if (isUnique)
            {
                container.RegisterType<View>(new ContainerControlledLifetimeManager());
                container.RegisterType<IViewModel, ViewModel>(new ContainerControlledLifetimeManager());
            }
            else
            {
                container.RegisterType<View>();
                container.RegisterType<IViewModel, ViewModel>(); 
            }
        }
    }
}
